package com.zlt.appparking.controller;


import com.zlt.appparking.entity.Camera;
import com.zlt.appparking.service.AccessRecordService;
import com.zlt.appparking.utils.FileUploadUtil;
import com.zlt.appparking.utils.ImgUtils;
import com.zlt.appparking.utils.ImgValueUtil;
import com.zlt.appparking.vo.AccessRecordVO;
import com.zlt.appparking.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("carRecord")
public class CarRecordController {

    @Autowired
    AccessRecordService accessRecordService;

    @PostMapping("carRecord")
    public Result carRecord(@RequestParam("file")MultipartFile img, HttpServletRequest request){
        int id =Integer.parseInt(request.getParameter("id"));
        byte[] carImg = new byte[0];

        String root = getClass().getClassLoader().getResource("static/files").getPath();
        new FileUploadUtil().upload(img,root);
        String path= root +"/"+img.getResource().getFilename();
        carImg = ImgUtils.file2byte(path);
        String carNum = ImgValueUtil.plateLicense(carImg);//拿到车牌号

//        String qnyPath = new FileUploadUtil().uploadQNY(img);
//        System.out.println("七牛云图片地址："+qnyPath);

        Object o = accessRecordService.addRecord(carNum, id);
        if("1".equals(o+"")){
            return Result.ok("车辆进入停车场");
        }else if("0".equals(o+"")){
            return Result.error("未知错误,请联系管理员处理");
        }else if("车位已满".equals(o+"")){
            return Result.error("车位已满");
        }else
            return Result.ok("车辆已出停车场",o);
    }

    @PostMapping("select")
    public Result selectRecord(String carNum){
        List<AccessRecordVO> accessRecordVOList = accessRecordService.selectRecord(carNum);
        return Result.ok("查询成功",accessRecordVOList);
    }

}
